//验证用户名是否已经存在
function checkUserName(obj) {
    if (obj.value === "" || obj.value == undefined){
        $('#msg').css("display","block");
        $('#msgTitle').html("用户名不合法").removeClass("alert-success").addClass("alert-danger");
        $('#regBtn').addClass("disabled")
        return false;
    }else {
        $.ajax({
            url: contextPath + "/user/checkUserName",
            method: "post",
            data: {"username":obj.value},
            success: function (data) {
                $('#msg').css("display","block");
                if (data === "102"){//用户存在
                    $('#msgTitle').html("用户名已被注册").removeClass("alert-success").addClass("alert-danger");
                    $('#regBtn').addClass("disabled")
                } else {
                    $('#msgTitle').html("用户名可以注册").removeClass("alert-danger").addClass("alert-success");
                    $('#regBtn').removeClass("disabled")
                    return true;
                }
            }
        })
        return true;
    }
}

//密码和空字段的校验
function checkPassword() {
    var pwd = $('#pwd').val();
    var rePwd = $('#rePwd').val();

    if (pwd == "" || pwd == undefined || pwd != rePwd){
        $('.pwd').show();
        return false;
    }else {
        $('.pwd').hide();
        return true;
    }
}

function checkNull(){
    var email = $('#email').val();
    var phone = $('#phone').val();
    var company = $('#company').val();
    if (email == "" || email == undefined || phone == "" || phone == undefined || company == "" || company == undefined){
        swal("提示信息", "请填写完整的信息！", "warning")
        return false;
    }else {
        var reg = /\w+[@]{1}\w+[.]\w+/;
        if (!reg.test(email)){
            swal("提示信息", "请输入正确的email！", "warning")
            $("#email").focus();
            return false;
        }
        var reg = /^1[345789]\d{9}$/;
        if (!reg.test(phone)){
            swal("提示信息", "请输入正确的手机号！", "warning")
            $("#phone").focus();
            return false;
        }
        return true;
    }
}

//用户注册
function register() {
    if (!checkUserName($('#regName')[0]) || !checkPassword() || !checkNull()){
        return;
    }
    else  {
        $.ajax({
            url: contextPath + "/user/register",
            method: "post",
            data: $("#regForm").serialize(),
            success: function (data) {
                if (data === "success"){
                    swal("提示信息", "注册成功，请登录", "success")
                }
                $("#registerModel").modal("hide");
            }
        })
    }

}

//用户登录
function login(){
    var datas = $("#loginForm").serialize();
    $.ajax({
        url: contextPath + "/user/login",
        method: "post",
        data: datas,
        success: function (data) {
            $('#noUser').css("display","none");
            $('#errorPwd').css("display","none");
            if (data === "100"){
                $("#loginModal").modal("hide");
                swal("提示信息", "登录成功", "success")
                window.location.href = contextPath + "/book/index";
            }else if (data == "101"){
                $('#noUser').css("display","block");
            }else {
                $('#errorPwd').css("display","block");
            }
        }
    })
}

//图书的搜索
function search() {
    var searchName = $("#bookName").val();
    if (searchName == "" || searchName == null){
        swal("提示信息", "请输入搜索的内容！", "warning")
        return false;
    }else {
       return true;
    }
}